package indi.caibo.service_edu.controller;


import indi.caibo.commonutils.JwtUtils;
import indi.caibo.commonutils.R;
import indi.caibo.service_edu.service.EduCourseCollectService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 课程收藏 前端控制器
 * </p>
 *
 * @author Bo Cai
 * @since 2021-12-21
 */
@Api(tags = {"课程收藏模块"})
@RestController
@RequestMapping("/edu-service/course-collect")
public class EduCourseCollectController {
    @Autowired
    EduCourseCollectService courseCollectService;

    @ApiOperation(value = "收藏课程")
    @GetMapping("{courseId}")
    public R collectCourse(
            @ApiParam(name = "courseId", value = "课程ID", required = true)
            @PathVariable String courseId,
            HttpServletRequest request
    ) {
        String memberId = JwtUtils.getMemberIdByJwtToken(request);
        courseCollectService.collect(memberId, courseId);
        return R.ok().message("课程收藏成功!");
    }

    @ApiOperation(value = "判断课程是否被当前用户收藏")
    @GetMapping("isCollected/{courseId}")
    public R isCollected(
            @ApiParam(name = "courseId", value = "课程ID", required = true)
            @PathVariable String courseId,
            HttpServletRequest request
    ) {
        String memberId = JwtUtils.getMemberIdByJwtToken(request);
        Boolean isCollected = courseCollectService.isCollected(memberId, courseId);
        return R.ok().data("isCollected", isCollected);
    }

    @ApiOperation(value = "取消收藏课程")
    @GetMapping("cancelCollect/{courseId}")
    public R cancelCollect(
            @ApiParam(name = "courseId", value = "课程ID", required = true)
            @PathVariable String courseId,
            HttpServletRequest request
    ) {
        String memberId = JwtUtils.getMemberIdByJwtToken(request);
        courseCollectService.cancelCollect(memberId, courseId);
        return R.ok().message("取消收藏成功！");
    }
}

